package com.guigu.buy.controller;

import com.github.pagehelper.PageInfo;
import com.guigu.buy.dto.Pro_Supplier;
import com.guigu.buy.mapper.SupplierMapper;
import com.guigu.buy.pojo.ProductSupplier;
import com.guigu.buy.service.IProductSupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author com.guigu
 * @since 2024-05-15
 */
@CrossOrigin
@Controller
@RequestMapping("/buy/productSupplier")
public class ProductSupplierController {
    @Autowired
    private SupplierMapper supplierMapper;
    @Autowired
    private IProductSupplierService productSupplierService;

    //查询供应商中间表

    @RequestMapping("/commodity/{pageSize}/{pageNo}/{uid}")
    @ResponseBody
    public PageInfo<Pro_Supplier> commodity(@PathVariable int uid,
                                            @PathVariable int pageSize,
                                            @PathVariable int pageNo)
    {

        return productSupplierService.commodity(uid,pageNo, pageSize);
    }

    //根据id显示维护对话框

    @RequestMapping("/showdisplay/{id}")
    @ResponseBody
    public ProductSupplier SupplierId(@PathVariable int id ) {
        return productSupplierService.getById(id);
    }

    //确定修改

    @RequestMapping("/weihu")
    @ResponseBody
    public String weihu(@RequestBody ProductSupplier productSupplier) {
        boolean flag = this.productSupplierService.updateById(productSupplier);
        if (flag) {

            return "ok";
        }
        return "error";
    }

    //点击删除

    @RequestMapping("/deletes/{id}")
    @ResponseBody
    public String deletes(@PathVariable int id){
        boolean warehouse=this.productSupplierService.removeById(id);
        if (warehouse){
            return  "ok";
        }
        return "error";
    }




    //点击确定供应

    @RequestMapping("saveStorageCellIdApp/{userId}")
    @ResponseBody
    public String saveStorageCellIdApp(@PathVariable int userId,
                                       @RequestBody ProductSupplier productSupplier){
        int supplierId=supplierMapper.querySupplierId(userId);
        productSupplier.setId(null);
        //添加
        productSupplier.setSupplierId(supplierId);
        boolean flag=this.productSupplierService.save(productSupplier);
        if (flag){
            return "ok";
        }
        return "error";
    }


}